package com.application.sistercityapplication.main;

import java.util.ArrayList;
import java.util.List;

import com.application.sistercityapplication.dto.GalleryDto;
import com.application.sistercityapplication.dto.PictureDto;
import com.application.sistercityapplication.gallery.AppConstant;
import com.application.sistercityapplication.gallery.GridViewImageAdapter;
import com.application.sistercityapplication.gallery.Utils;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class GalleryPageFragment extends Fragment {
	private ProgressBar pBar;
	private View v,v2;
	private LayoutInflater inflater2;
	private FrameLayout mainLayout;
	private String countryTitle;
	private int cId;
	private GridView gridView;
	private int columnWidth;
	private ImageView menuToggle;
	private SlidingMenu sm;
	private Utils utils;
	private List<PictureDto> imagePaths = new ArrayList<PictureDto>();
	private GridViewImageAdapter adapter;
	public GalleryPageFragment(int country_id,String country_title,SlidingMenu sm, List<PictureDto> pictures){
		this.cId = country_id;
		this.countryTitle = country_title;
		this.sm = sm;
		this.imagePaths = pictures;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		utils = new Utils(getActivity().getApplicationContext());
		v = inflater.inflate(R.layout.new_activity_city_information_main, container,false);

		mainLayout = (FrameLayout) v.findViewById(R.id.frameForInflate);
		inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v2 = inflater2.inflate(R.layout.activity_gallery_main, null);
		ImageView back_icon = (ImageView) v2.findViewById(R.id.back_icon);
		menuToggle = (ImageView) v2.findViewById(R.id.menuToggle);
		menuToggle.setVisibility(View.INVISIBLE);
		gridView = (GridView) v2.findViewById(R.id.grid_view);
		pBar = (ProgressBar) v2.findViewById(R.id.progress);
		mainLayout.addView(v2);
		
		
		InitilizeGridLayout();
		new DownLoadGalleryTask(getActivity().getApplicationContext(),imagePaths,getActivity(),pBar).execute();
		menuToggle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sm.toggle();
			}
		});
		back_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Fragment newContent = new MainCatImageActivity(cId,countryTitle,sm);
				switchFragment(newContent);
			}
		});
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	
	private void InitilizeGridLayout() {
		Resources r = getResources();
		float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,AppConstant.GRID_PADDING, r.getDisplayMetrics());

		columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

		gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
		gridView.setColumnWidth(columnWidth);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setPadding((int) padding, (int) padding, (int) padding,
				(int) padding);
		gridView.setHorizontalSpacing((int) padding);
		gridView.setVerticalSpacing((int) padding);
	}
	public class DownLoadGalleryTask extends AsyncTask<String, Void, List<PictureDto>> {
		ProgressDialog pDialog;
		private Context context;
		public Activity _activity;
		private ProgressBar _resid;

		public DownLoadGalleryTask(Context context, List<PictureDto> view,Activity act,ProgressBar resid) {
			super();
			this.context = context;
			this._activity = act;
			this._resid = resid;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
//			pDialog = new ProgressDialog(MainHomePageViewActivity.this);
//			pDialog.setMessage("Loading Content...");
//			pDialog.show();
			_resid.setVisibility(View.VISIBLE);
		}

		@Override
		protected List<PictureDto> doInBackground(String... params) {
			
			return imagePaths;
		}

		@Override
		protected void onPostExecute(List<PictureDto> result) {
			super.onPostExecute(result);
//			if (null != pDialog && pDialog.isShowing()) {
//				pDialog.dismiss();
//			}
			menuToggle.setVisibility(View.VISIBLE);
			_resid.setVisibility(View.GONE);
			if (result != null) {
				adapter = new GridViewImageAdapter(getActivity(), result,columnWidth);
				gridView.setAdapter(adapter);
			} else {
				(Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG)).show();
			}

		}
	}
	
}
